package com.suneee.base.util;

import java.io.ByteArrayInputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.suneee.core.type.json.JsonUtils;
import com.suneee.core.type.json.SpecialDateDerializer;
import com.suneee.core.type.json.SpecialStringDerializer;
import com.suneee.core.type.json.SpecialTimestampDerializer;
import com.suneee.store.service.impl.ShopStockServiceImpl;

public class JsonUtil {
	
	private static final Logger log = Logger.getLogger(JsonUtil.class);

	@SuppressWarnings("unchecked")
	public static Map<String,String> parse(String ruleInfoStr){
		Map<String,String> map = new HashMap<String, String>();
		try{
			ObjectMapper mapper = new ObjectMapper();
			map = mapper.readValue(ruleInfoStr, Map.class);
		}catch(Exception e){
			log.error("第三方接口公共入参转换异常");
		}
		return map;
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String,Object> parseToObj(String ruleInfoStr){
		Map<String,Object> map = new HashMap<String, Object>();
		try{
			ObjectMapper mapper = new ObjectMapper();
			map = mapper.readValue(ruleInfoStr, Map.class);
		}catch(Exception e){
			log.error("第三方接口公共入参转换异常");
		}
		return map;
	}
	
	public static JavaType getCollectionType(ObjectMapper mapper,Class<?> collectionClass, Class<?>... elementClasses) {   
        return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);   
    }
	

	public static  String  getValueByFind(JsonNode node, String path) throws Exception {  
	    List<String> values = new ArrayList<String>();  
	    node.findValuesAsText(path, values);  
	    String str = values.get(0).toString();
	    return str;  
	} 
	
	public static List<String> getValueListByFind(JsonNode node, String path) throws Exception {  
	    List<String> values = new ArrayList<String>();  
	    //JsonNode find = node.findValue(path);
	    
	    node.findValuesAsText(path, values); 

	   // find.asText();
	   // System.out.println(find);
	    
	    System.out.println(values);
	    return values;  
	} 
	
	
	/** 
	 * 查找当前Node中第一个匹配的值 
	 *  
	 * @param node 
	 * @param path 
	 * @return 
	 * @throws Exception 
	 */  
	public static int getFirstValueByFind(JsonNode node, String path) throws Exception {  
	    int value = node.findValue(path).asInt();  
	    System.out.println(value);  
	    return value;  
	}  
	
	public static Object fromJson(String json,Class cls){
		try{
			ByteArrayInputStream is=new ByteArrayInputStream(json.getBytes()); 
//			ObjectMapper mapper = JsonUtils.reader();
			ObjectMapper mapper = JsonUtil.reader();
			JsonNode root = mapper.readTree(is);
			Object v= mapper.convertValue(root,cls); 
	        return v;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static ObjectMapper reader() {
		ObjectMapper  reader = new ObjectMapper();
        //reader.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        SimpleModule module = new SimpleModule();
        module.addDeserializer(String.class, new SpecialStringDerializer());
        module.addDeserializer(Date.class, new SpecialDateDerializer());
        module.addDeserializer(Timestamp.class, new SpecialTimestampDerializer());
        reader.registerModule(module);
        return reader;
    }
	
	public static void main(String[] args) throws Exception{
//		Map<String,String> map = JsonUtil.parse("[{\"ruleCode\":\"23055821\",\"displayName\":\"appKey\",\"regex\":\"\",\"required\":\"true\",\"tip\":\"\"},"
//				+ "{\"ruleCode\":\"a9fee0f44840016763d3c9a145f03982\",\"displayName\":\"appSecret\",\"regex\":\"\",\"required\":\"true\",\"tip\":\"\"}]");
//		System.out.println(map.get("appKey"));
		
		
		
		
//		InputStream is = new FileInputStream(new File("G:\\json.txt"));
//		InputStreamReader read = new InputStreamReader(is);
//		BufferedReader bufferedReader = new BufferedReader(read);
//		String lineTxt = null;
//		StringBuffer sb = new StringBuffer();
//		while ((lineTxt = bufferedReader.readLine()) != null) {
//			sb.append(lineTxt);
//		}
//		read.close();
//
//		ObjectMapper mapper = new ObjectMapper();
//		JsonNode readTree = mapper.readTree(sb.toString());
//		getValueListByFind(readTree,"price");
		
		//getValueListByFind(readTree,"price");
		String json = "{\"status_of\":\"正常\",\"size_of\":\"1*2.1*0.3 米\", \"zxs_of\":200,\"suite_yes\":\"是\"}";
		Map<String,Object> map = new HashMap<String, Object>();
		Map<String,Object> map1 =  (Map<String, Object>) JsonUtil.fromJson(json, map.getClass());
//		System.out.println(map1);
		Integer size_of = (Integer) map1.get("zxs_of");
		System.out.println(size_of);
		 
	}
}
